<?php

class Shop_model extends CI_Model
{

    public function __construct()
    {

        $this->load->database();
        $this->load->helper("common");
    }

    // 获取今日总佣金
    public function getTodayProperty($shop_id)
    {

      //今天收获的佣金减掉退款的佣金
      $s_total = $this->db->query("
                  select
                    SUM(money) as total
                  from
                    app_property_detail
                  where
                    to_days(created_at) = to_days(now()) and source in (1,2) and shop_id = '$shop_id'
                ")->row()->total;

      $z_total = $this->db->query("
                  select
                    SUM(money) as total
                  from
                    app_property_detail
                  where
                    to_days(created_at) = to_days(now()) and source = 4 and shop_id = '$shop_id'
                ")->row()->total;

       return $s_total - $z_total;
    }

    //  获取商户可提现佣金
    public function getNormal($shop_id)
    {

      return  $this->db->query("
                select
                  normal
                from
                  app_property
                where
                  shop_id = '$shop_id'
              ")->row()->normal;
    }

    //  获取商户总客户
    public function getAllUser($shop_id)
    {

      return  $this->db->query("
                select
                  count(*) as allUser
                from
                  `app_shop_user`
                where
                  shop_id = '$shop_id'
              ")->row()->allUser;
    }

    // 获取今日新客户
    public function getTodayNewUser($shop_id)
    {

      return  $this->db->query("
                select
                  count(*) as newUser
                from
                  `app_shop_user`
                where
                  to_days(created_at) = to_days(now()) and shop_id = '$shop_id'
              ")->row()->newUser;
    }

    //  获取客户退款申请数量
    public function getRefund($shop_id)
    {

      return  $this->db->query("
                select
                  count(*) as refund
                from
                  `app_order`
                where
                  shop_id = '$shop_id' and refund_status = 0
              ")->row()->refund;
    }

    // 获取商户的新用户佣金或者销售佣金
    public function getProperty($shop_id,$type)
    {

      if($type == 2){
        //新用户佣金
        return  $this->db->query("
                  select
                    a.detail_id as orderId, a.source as source ,d.nick_name as name, b.order_bn as order_bn, a.created_at as createdTime, c.name as shopName, d.created_at as resisterTime, a.money as money
                  from
                    app_property_detail a, app_order b, app_shop c, app_user d
                  where
                    a.source = 2 and a.shop_id = '$shop_id' and a.detail_id = b.id and b.shop_id = c.id and b.user_id = d.id
                  order by
                    a.created_at desc
                ")->result_array();
      }else{
        //销售佣金
        return  $this->db->query("
                  select
                    a.detail_id as orderId, a.source as source , d.nick_name as name, b.order_bn as order_bn, a.created_at as createdTime, d.created_at as resisterTime, e.name as registerShopName, a.money as money
                  from
                    app_property_detail a, app_order b, app_shop_user c, app_user d, app_shop e
                  where
                    a.source = 1 and a.shop_id = '$shop_id' and a.detail_id = b.id and b.user_id = d.id and b.user_id = c.user_id and e.id = c.shop_id
                  order by
                    a.created_at desc
                ")->result_array();
      }
    }

    // 获取商户退款列表
    public function getReFundList($shop_id)
    {

      return $this->db->query("
                select
                  a.id as orderId, a.order_bn as order_bn, b.remark as remark, b.detail as detail ,b.pic_1 ,b.pic_2 ,b.pic_3
                from
                  app_order a right join app_refund b
                on
                  a.id = b.order_id and b.status = 0
                where
                  a.refund_status = 0 and a.shop_id = '$shop_id'
                order by
                  b.created_at desc
            ")->result_array();
    }

    //获取商户库存详情

    public function getLackManage($shop_id)
    {
      return $this->db->query("
                select
                  c.title as name, c.goods_bn as goodsBn  , c.pic_4 as pic, a.num as goodsNum, b.name as shopName, f.created_at as fromTime, f.updated_at as endTime
                from
                  app_shop_goods a
                left join
                  app_shop b
                on
                  a.shop_id = b.id
                left join
                  app_goods c
                on
                  a.goods_id = c.id
                left join
                  (select
                    d.goods_id as goodsid ,e.created_at as created_at ,e.updated_at as updated_at
                  from
                    app_restock_detail d
                  left join
                    app_restock e
                  on
                    d.restock_id = e.id
                  where
                    e.shop_id = '$shop_id' and e.status = 1
                  group by
                    d.goods_id
                  order by
                    e.id desc) as f
                on
                  a.goods_id = f.goodsid
                where
                  a.shop_id = '$shop_id'
                order by a.id desc
              ")->result_array();
    }

    // public function getOrderInfoByUserId($user_id,$shop_id)
    // {
    //   //获取本店订单数量
    //
    //   //获取订单总量
    //
    //   //
    // }
}
